package sz.szsmk.citizencard.ui.activity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.res.Resources;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chenenyu.router.annotation.Route;
import java.util.ArrayList;
import java.util.List;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.CareModeActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryAllFunctionNewRes;
import sz.szsmk.citizencard.mvp.contract.ICareModeContact;
import sz.szsmk.citizencard.mvp.presenter.CareModePresenter;
import sz.szsmk.citizencard.ui.adapter.CareModeAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.JumpThirdView;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.PermissionUtil;
import sz.szsmk.citizencard.util.SecureSharedPreferences;

/**
 * Copyright (c)2021/5/20
 * author: hxh
 * created on: 2021/5/20 14:34
 * description:
 */


@Route("careModeActivity")
public class CareModeActivity extends IBaseActivity<CareModePresenter,
        CareModeActivityBinding> implements ICareModeContact.ICareModeView, View.OnClickListener {
    private View back;
    private com.suke.widget.SwitchButton switchButton;
    private TextView moreService;
    private boolean careMode;
    private CareModeAdapter adapter;
    private ArrayList<QueryAllFunctionNewRes.ClassListBean.FunctionListBean> beans;
    private ImageView backit;

    @Override
    protected int attachLayoutRes() {
        return R.layout.care_mode_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        careMode = SecureSharedPreferences.getBoolean("careMode", false);
        switchButton=dataBinding.switchButton;
        dataBinding.backCareMode.settingTitle.setText("关怀模式");
        back=dataBinding.backCareMode.back;
        RecyclerView recyclerView=dataBinding.careModeList;
        moreService=dataBinding.moreService;
        backit=dataBinding.ptms;


        mPresenter.getViewData();
        beans = new ArrayList<>();
        adapter=new CareModeAdapter(this, beans, position -> {
            if (beans.get(position).getLink_url().equals("rechargeActivity")){
                JumpUtils.jump(CareModeActivity.this,beans.get(position).getLink_url(),1);
            }else
            JumpThirdView.baseToJumpView(CareModeActivity.this,beans.get(position).getLink_url(),"",0,"");
        });
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        onclickViews();
    }

    private void onclickViews() {
        back.setOnClickListener(this);
        moreService.setOnClickListener(this);
        backit.setOnClickListener(this);
        //滑动按钮
        switchButton.setChecked(careMode);
        switchButton.toggle();
        switchButton.toggle(false);
        switchButton.setShadowEffect(true);
        switchButton.setEnabled(true);
        switchButton.setEnableEffect(false);
        switchButton.setOnCheckedChangeListener((view, isChecked) -> {
            if (isChecked){
                SecureSharedPreferences.putBoolean("careMode",true);
                Log.e("careModetrue",SecureSharedPreferences.getBoolean("careMode", false)+"");
            }else {
                SecureSharedPreferences.putBoolean("careMode",false);
                Log.e("careModefalse",SecureSharedPreferences.getBoolean("careMode", false)+"");
            }
        });
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back || id == R.id.ptms) {
            finish();
        } else if (id == R.id.more_service) {
            setResult(3004);
            finish();
        }
    }


    @Override
    public void getViewDataSucc(QueryAllFunctionNewRes response) {
        //这个集合是最终需要显示的数据。
        ArrayList<QueryAllFunctionNewRes.ClassListBean.FunctionListBean> temp = new ArrayList<>();
        QueryAllFunctionNewRes.ClassListBean.FunctionListBean functionListBean = new QueryAllFunctionNewRes.ClassListBean.FunctionListBean();
        functionListBean.setFunction_name("充值");
        String mipmapToUri = getMipmapToUri();
        functionListBean.setIcon_url(mipmapToUri);
        functionListBean.setLink_url("rechargeActivity");
        temp.add(functionListBean);



        List<QueryAllFunctionNewRes.ClassListBean.FunctionListBean> function_list = response.getClass_list().get(3).getFunction_list();
        for (int i = 0; i < function_list.size(); i++) {
            if (function_list.get(i).getFunction_name().equals("余额查询")||
                    function_list.get(i).getFunction_name().equals("服务网点")||
                    function_list.get(i).getFunction_name().equals("消费商家")){
                temp.add(function_list.get(i));

            }
        }


        List<QueryAllFunctionNewRes.ClassListBean> class_list = response.getClass_list();
        QueryAllFunctionNewRes.ClassListBean listBean = class_list.get(2);
        for (int i = 0; i <listBean.getFunction_list().size(); i++) {
            if (listBean.getFunction_list().get(i).getFunction_name().equals("休闲年卡")||
                    listBean.getFunction_list().get(i).getFunction_name().equals("园林年卡")){
                temp.add(listBean.getFunction_list().get(i));
            }
        }

        beans.addAll(temp);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getViewDataFail(QueryAllFunctionNewRes response) {

    }

    //图片ID转URI
    private String getMipmapToUri() {

        Resources r = getResources();
        Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + r.getResourcePackageName(R.mipmap.icon_home_recharge) + "/"
                + r.getResourceTypeName(R.mipmap.icon_home_recharge) + "/"
                + r.getResourceEntryName(R.mipmap.icon_home_recharge));

        return uri.toString();
    }
}
